class SectionController < ApplicationController
  before_filter :login_required, :except=>:index

  in_place_edit_for :section, :title 

  def index
    @sections = Section.find(:all)
  end

  def authorized?
    current_user.is_administrator
  end

  def admin
    @sections  = Section.find(:all)
    unless params[:id]
      params[:id] = 1
    end
    @section = Section.find(params[:id])
    render :action => "admin", :layout => "admin"
  end

  def new
  end

  def create
  end
  
  def edit
    @section = Section.find(params[:id])
    @sections = Section.find(:all)
    if request.xml_http_request?
      render :layout => false
    end
  end

  def destroy
    s = Section.destroy(params[:id])
    if request.xml_http_request?
      render :update do |page|
        page.Effect.Fade("admin_section_#{params[:id]}")
      end
    else
      flash[:notice] = "Section `#{s.title}` has been succesfully destroyed."
      redirect_to :controller=>'section', :action=>'admin'
    end
  rescue ActiveRecord::RecordNotFound
    message = "Tried to destroy invalid section (#{params[:id]})"
    logger.error(message)
    flash[:warning] = message
    redirect_to :controller => 'section', :action => "admin"
  end
end
